﻿using ImageManager.Common;
using ImageManager.DBAccess;
using ImageManager.ImageApi;
using ImageManager.ImageApi.AuthenticationInjection;
using ImageManager.ImageService;
using ImageManager.Model;
using ImageManager.MyExcep;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace ImageManager.Controllers
{
    public class SeriesController : MyBaseController<SeriesModel>
    {
        protected override BaseServiceProvider<SeriesModel> getSp()
        {
            return ServiceLoader.GetService<SeriesServiceProvider>();
        }
        [HttpGet, MyAuthorize(AuthorizeLevelEnum.AllUser)]

        public ResponseData<string> Disable(string pk)
        {
            var sp = getSp();
            var v = sp.Get(pk);
            v.Disable = true;
            sp.Save(v);
            return new ResponseData<string>();
        }
        [HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<SeriesModel> BatchSave(dynamic data)
        {
            string names = data.names.ToString();
            var nameList = names.Split(new string[] { "\n" ,"\t","\r\n" },StringSplitOptions.RemoveEmptyEntries);
            var series = new List<SeriesModel>();
            string message = "";
            foreach (var name in nameList)
            {
                SeriesModel seriesModel = new SeriesModel();
                seriesModel.SeriesName = name;
                try
                {
                    getSp().Save(seriesModel);
                    series.Add(seriesModel);
                }
                catch (MyException ex)
                {
                    message += "名称=" + seriesModel.SeriesName + "保存失败:"+ex.Message;
                    message += "\r\n";
                }
                catch (Exception ex)
                {
                    message += "名称=" + seriesModel.SeriesName + "保存失败";
                }
            }
            var result = new ResponseData<SeriesModel>(series);
            result.Message = message;
            return result;
        }
    }
}
